package zct.proj.equipModel;

import java.util.HashMap;

import android.graphics.Color;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Paint.Align;
import zct.proj.pojo.EquipmentInfo;
import zct.proj.util.Const;

public class DCModel extends BaseModel {

	private HashMap<String, DCInfo> infoMap = new HashMap<String, DCInfo>();
	
	public DCModel(float scale, int cellWidth, int cellHeight) {
		super(scale, cellWidth, cellHeight);
	}

	/**
	 * 小道岔的四个方向画法
	 * @param dt 数据
	 * @return
	 */
	private DCInfo getXdcPath(EquipmentInfo dt) {
		int fullWidth = dt.getWidth() * xSpacing / 2;
		int fullHeight = dt.getHeight() * ySpacing / 2;
		int yDis = (int) (0.6f * ySpacing);
		int xDis = (int) (0.6f * xSpacing);
		DCInfo dInfo = null;
		Path curve1 = new Path();
		Path curve2 = new Path();
		Path direct1 = new Path();
		Path direct2 = new Path();
		switch (dt.getDir()) {
		case Const.DIR_NORMAL:
			// 起始点
			curve1.moveTo(fullWidth, fullHeight - yDis);
			// 小线头
			curve1.lineTo(fullWidth + xDis, fullHeight - yDis);
			// 斜线
			curve1.lineTo(fullWidth * 2, 0);
			// 直线
			direct1.moveTo(fullWidth, fullHeight);
			direct1.lineTo(fullWidth * 2, fullHeight);
			
			// 反位
			curve2.moveTo(fullWidth, fullHeight);
			curve2.lineTo(fullWidth * 2, 0);
			direct2.moveTo(fullWidth + xDis, fullHeight);
			direct2.lineTo(fullWidth * 2, fullHeight);
			
			dInfo = new DCInfo(direct1, curve1, direct2, curve2);
			// 表示文字
			if (dt.getNameVisible() && dt.getDisp() != null) {
				dInfo.setFont(new Point(fullWidth, fullHeight + ySpacing), Align.LEFT);
			}
			break;
		case Const.DIR_VCOPY:
			// 起始点
			curve1.moveTo(fullWidth, fullHeight + yDis);
			// 小线头
			curve1.lineTo(fullWidth + xDis, fullHeight + yDis);
			// 斜线
			curve1.lineTo(fullWidth * 2, fullHeight * 2);
			// 直线
			direct1.moveTo(fullWidth, fullHeight);
			direct1.lineTo(fullWidth * 2, fullHeight);
			
			// 反位
			curve2.moveTo(fullWidth, fullHeight);
			curve2.lineTo(fullWidth * 2, fullHeight * 2);
			direct2.moveTo(fullWidth + xDis, fullHeight);
			direct2.lineTo(fullWidth * 2, fullHeight);
			
			dInfo = new DCInfo(direct1, curve1, direct2, curve2);
			// 表示文字
			if (dt.getNameVisible() && dt.getDisp() != null) {
				dInfo.setFont(new Point(fullWidth, fullHeight - Const.TEXT_DRAW_DISTANCE), Align.LEFT);
			}
			break;
		case Const.DIR_ROTATE:
			curve1.moveTo(fullWidth, fullHeight + yDis);
			// 小线头
			curve1.lineTo(fullWidth - xDis, fullHeight + yDis);
			// 斜线
			curve1.lineTo(0, fullHeight * 2);
			// 直线
			direct1.moveTo(0, fullHeight);
			direct1.lineTo(fullWidth, fullHeight);
			
			// 反位
			curve2.moveTo(fullWidth, fullHeight);
			curve2.lineTo(0, fullHeight * 2);
			direct2.moveTo(0, fullHeight);
			direct2.lineTo(fullWidth - xDis, fullHeight);
			
			dInfo = new DCInfo(direct1, curve1, direct2, curve2);
			// 表示文字
			if (dt.getNameVisible() && dt.getDisp() != null) {
				dInfo.setFont(new Point(fullWidth, fullHeight - Const.TEXT_DRAW_DISTANCE), Align.RIGHT);
			}
			break;
		case Const.DIR_HCOPY:
			// 起始点
			curve1.moveTo(fullWidth, fullHeight - yDis);
			// 小线头
			curve1.lineTo(fullWidth - xDis, fullHeight - yDis);
			// 斜线
			curve1.lineTo(0, 0);
			// 直线
			direct1.moveTo(fullWidth, fullHeight);
			direct1.lineTo(0, fullHeight);
			
			// 反位
			curve2.moveTo(fullWidth, fullHeight);
			curve2.lineTo(0, 0);
			direct2.moveTo(fullWidth - xDis, fullHeight);
			direct2.lineTo(0, fullHeight);
			
			dInfo = new DCInfo(direct1, curve1, direct2, curve2);
			// 表示文字
			if (dt.getNameVisible() && dt.getDisp() != null) {
				dInfo.setFont(new Point(fullWidth, fullHeight + ySpacing), Align.RIGHT);
			}
			break;
		}
		return dInfo;
	}
	
	/**
	 * 大道岔的四个方向画法
	 * @param dt 数据
	 * @return
	 */
	private DCInfo getDdcPath(EquipmentInfo dt) {
		int fullWidth = xSpacing * 2;
		int fullHeight = ySpacing * 2;
		int yDis = (int) (0.3f * ySpacing);
		int xDis = (int) (0.3f * xSpacing);
		DCInfo dInfo = null;
		Path curve = new Path();
		Path direct = new Path();
		Path dirAfter = new Path();
		switch (dt.getDir()) {
		case Const.DIR_NORMAL:
			// 起始点
			direct.moveTo(0, fullHeight);
			// 斜线
			direct.lineTo(xSpacing, ySpacing);
			// 直线
			dirAfter.moveTo(xSpacing, ySpacing);
			dirAfter.lineTo(fullWidth, ySpacing);
			// 小线头
			curve.moveTo(xSpacing, ySpacing - yDis);
			curve.lineTo(xSpacing + xDis, ySpacing - yDis);
			// 斜线
			curve.lineTo(fullWidth, 0);
			dInfo = new DCInfo(direct, curve, dirAfter);
			// 表示文字
			if (dt.getNameVisible() && dt.getDisp() != null) {
				dInfo.setFont(new Point(0, ySpacing), Align.LEFT);
			}
			break;
		case Const.DIR_VCOPY:
			// 起始点
			direct.moveTo(0, 0);
			// 斜线
			direct.lineTo(xSpacing, ySpacing);
			// 直线
			dirAfter.moveTo(xSpacing, ySpacing);
			dirAfter.lineTo(fullWidth, ySpacing);
			// 小线头
			curve.moveTo(xSpacing, ySpacing + yDis);
			curve.lineTo(xSpacing + xDis, ySpacing + yDis);
			// 斜线
			curve.lineTo(fullWidth, fullHeight);
			dInfo = new DCInfo(direct, curve, dirAfter);
			// 表示文字
			if (dt.getNameVisible() && dt.getDisp() != null) {
				dInfo.setFont(new Point(0, ySpacing * 2), Align.LEFT);
			}
			break;
		case Const.DIR_ROTATE:
			// 起始点
			direct.moveTo(fullWidth, 0);
			// 斜线
			direct.lineTo(xSpacing, ySpacing);
			// 直线
			dirAfter.moveTo(xSpacing, ySpacing);
			dirAfter.lineTo(0, ySpacing);
			// 小线头
			curve.moveTo(xSpacing, ySpacing + yDis);
			curve.lineTo(xSpacing - xDis, ySpacing + yDis);
			// 斜线
			curve.lineTo(0, fullHeight);
			dInfo = new DCInfo(direct, curve, dirAfter);
			// 表示文字
			if (dt.getNameVisible() && dt.getDisp() != null) {
				dInfo.setFont(new Point(xSpacing, ySpacing * 2), Align.LEFT);
			}
			break;
		case Const.DIR_HCOPY:
			// 起始点
			direct.moveTo(fullWidth, fullHeight);
			// 斜线
			direct.lineTo(xSpacing, ySpacing);
			// 直线
			dirAfter.moveTo(xSpacing, ySpacing);
			dirAfter.lineTo(0, ySpacing);
			// 小线头
			curve.moveTo(xSpacing, ySpacing - yDis);
			curve.lineTo(xSpacing - xDis, ySpacing - yDis);
			// 斜线
			curve.lineTo(0, 0);
			dInfo = new DCInfo(direct, curve, dirAfter);
			// 表示文字
			if (dt.getNameVisible() && dt.getDisp() != null) {
				dInfo.setFont(new Point(xSpacing, ySpacing), Align.LEFT);
			}
			break;
		}
		return dInfo;
	}

	/**
	 * 垂直小道岔的四个方向画法
	 * @param dt 数据
	 * @return
	 */
	private DCInfo getCzXdcPath(EquipmentInfo dt) {
		int fullWidth = dt.getWidth() * xSpacing / 2;
		int fullHeight = dt.getHeight() / 2 * ySpacing;
		int yDis = (int) (0.3f * ySpacing);
		int xDis = (int) (0.3f * xSpacing);
		DCInfo dInfo = null;
		Path curve1 = new Path();
		Path direct1 = new Path();
		Path curve2 = new Path();
		Path direct2 = new Path();
		switch (dt.getDir()) {
		case Const.DIR_NORMAL:
			// 起始点
			curve1.moveTo(fullWidth - xDis, fullHeight);
			// 小线头
			curve1.lineTo(fullWidth - xDis, fullHeight - yDis);
			// 斜线
			curve1.lineTo(0, 0);
			// 直线
			direct1.moveTo(fullWidth, fullHeight);
			direct1.lineTo(fullWidth, 0);
			
			// 反位
			curve2.moveTo(fullWidth, fullHeight);
			curve2.lineTo(0, 0);
			direct2.moveTo(fullWidth, fullHeight);
			direct2.lineTo(fullWidth, yDis);
			
			dInfo = new DCInfo(direct1, curve1, direct2, curve2);
			// 表示文字
			if (dt.getNameVisible() && dt.getDisp() != null) {
				dInfo.setFont(new Point(fullWidth + Const.TEXT_DRAW_DISTANCE, fullHeight), Align.LEFT);
			}
			break;
		case Const.DIR_VCOPY:
			// 起始点
			curve1.moveTo(fullWidth - xDis, fullHeight);
			// 小线头
			curve1.lineTo(fullWidth - xDis, fullHeight + yDis);
			// 斜线
			curve1.lineTo(0, fullHeight * 2);
			// 直线
			direct1.moveTo(fullWidth, fullHeight);
			direct1.lineTo(fullWidth, fullHeight * 2);
			
			// 反位
			curve2.moveTo(fullWidth, fullHeight);
			curve2.lineTo(0, fullHeight * 2);
			direct2.moveTo(fullWidth, fullHeight);
			direct2.lineTo(fullWidth, fullHeight * 2 - yDis);
			
			dInfo = new DCInfo(direct1, curve1, direct2, curve2);
			// 表示文字
			if (dt.getNameVisible() && dt.getDisp() != null) {
				dInfo.setFont(new Point(fullWidth + Const.TEXT_DRAW_DISTANCE, fullHeight + ySpacing), Align.LEFT);
			}
			break;
		case Const.DIR_ROTATE:
			// 起始点
			curve1.moveTo(fullWidth + yDis, fullHeight);
			// 小线头
			curve1.lineTo(fullWidth + yDis, fullHeight + yDis);
			// 斜线
			curve1.lineTo(fullWidth * 2, fullHeight * 2);
			// 直线
			direct1.moveTo(fullWidth, fullHeight);
			direct1.lineTo(fullWidth, fullHeight * 2);
			
			// 反位
			curve2.moveTo(fullWidth, fullHeight);
			curve2.lineTo(fullWidth * 2, fullHeight * 2);
			direct2.moveTo(fullWidth, fullHeight);
			direct2.lineTo(fullWidth, fullHeight * 2 - yDis);
			
			dInfo = new DCInfo(direct1, curve1, direct2, curve2);
			// 表示文字
			if (dt.getNameVisible() && dt.getDisp() != null) {
				dInfo.setFont(new Point(fullWidth - Const.TEXT_DRAW_DISTANCE, fullHeight + ySpacing), Align.RIGHT);
			}
			break;
		case Const.DIR_HCOPY:
			// 起始点
			curve1.moveTo(fullWidth + yDis, fullHeight);
			// 小线头
			curve1.lineTo(fullWidth + yDis, fullHeight - yDis);
			// 斜线
			curve1.lineTo(fullWidth * 2, 0);
			// 直线
			direct1.moveTo(fullWidth, fullHeight);
			direct1.lineTo(fullWidth, 0);
			
			// 反位
			curve2.moveTo(fullWidth, fullHeight);
			curve2.lineTo(fullWidth * 2, 0);
			direct2.moveTo(fullWidth, fullHeight);
			direct2.lineTo(fullWidth, yDis);
			
			dInfo = new DCInfo(direct1, curve1, direct2, curve2);
			// 表示文字
			if (dt.getNameVisible() && dt.getDisp() != null) {
				dInfo.setFont(new Point(fullWidth - Const.TEXT_DRAW_DISTANCE, fullHeight), Align.RIGHT);
			}
			break;
		}
		return dInfo;
	}

	public DCInfo getDCInfo(EquipmentInfo sb) {
		
		String key = sb.getType() + "_" + sb.getDir() + "_" + sb.getWidth() + "_" + sb.getHeight();
		DCInfo dInfo = infoMap.get(key);
		if (dInfo != null) {
			return dInfo;
		}
		
		if (sb.getType() == Const.TYPE_DC_XDCZ
				|| sb.getType() == Const.TYPE_DC_XDCW) {
			// 道岔 小道岔定位为直  小道岔定位为弯
			dInfo = getXdcPath(sb);
		} else if (sb.getType() == Const.TYPE_DC_DDCZ
				|| sb.getType() == Const.TYPE_DC_DDCW) {
			// 道岔 大道岔定位为直  大道岔定位为弯
			dInfo = getDdcPath(sb);
		} else if (sb.getType() == Const.TYPE_DC_CZXDCZ
				|| sb.getType() == Const.TYPE_DC_CZXDCW) {
			// 道岔 垂直小道岔定位为直  垂直小道岔定位为弯
			dInfo = getCzXdcPath(sb);
		}
		infoMap.put(key, dInfo);
		return dInfo;
	}
}
